package etnorservice.etnor_service.controller;

import etnorservice.etnor_service.domain.dto.CommentDTO;
import etnorservice.etnor_service.domain.po.Comment;
import etnorservice.etnor_service.service.CommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/comments")
@Slf4j
public class CommentController {

    @Autowired
    private CommentService commentService;

    @PostMapping
    public ResponseEntity<CommentDTO> createComment(@RequestBody CommentDTO comment) {
        log.info("新增评论",comment);
        System.out.println(comment);
        return ResponseEntity.ok(commentService.createComment(comment));

    }

    @GetMapping("/api/{apiId}")
    public ResponseEntity<List<Comment>> getCommentsByApiId(@PathVariable Long apiId) {
        return ResponseEntity.ok(commentService.getCommentsByApiId(apiId));
    }

    @PutMapping("/{id}")
    public ResponseEntity<Comment> updateComment(@PathVariable Long id, @RequestBody Comment comment) {
        comment.setId(id);
        return ResponseEntity.ok(commentService.updateComment(comment));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteComment(@PathVariable Long id) {
        commentService.deleteComment(id);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/{id}")
    public ResponseEntity<Comment> getComment(@PathVariable Long id) {
        return ResponseEntity.ok(commentService.getCommentById(id));
    }
}